package com.example.zhidingyiview.zhidingyiview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.zhidingyiview.R;


public class DrawView extends View {

    private int mStartX;
    private int mStartY;
    private int mStopY;
    private int mStopX;
    private Paint mPaint;
    private final int mCx;
    private final int mCy;
    private final int mRadius;
    private final Paint mCirclePaint;
    private final Bitmap mBitmap;
    private final Path mPath;
    private final RectF mRectF;
    private final RectF mRectF1;
    private final Paint mArcPaint;

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        //1.画线
        mStartX = 50;
        mStartY = 50;
        mStopX = 700;
        mStopY = 700;

        //Paint :画笔
        mPaint = new Paint();
        mPaint.setStrokeWidth(30);
        mPaint.setColor(Color.parseColor("#ffffff"));

        //2. 画圆
        mCx = 450;
        mCy = 450;
        mRadius = 200;

        //3.空心圆
        mCirclePaint = new Paint();
        //空心画笔
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(30);
        mCirclePaint.setColor(Color.parseColor("#00ff00"));
        //去锯齿
        mCirclePaint.setAntiAlias(true);

        //4.画图片
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meizi3);

        //5. 画三角形(多边形)
        int x1 = 50;
        int y1 = 50;

        int x2 = 700;
        int y2 = 100;

        int x3 = 400;
        int y3 = 800;
        //路径
        mPath = new Path();
        //移动到某个点
        mPath.moveTo(x1,y1);
        //连线连到下一个点
        mPath.lineTo(x2,y2);
        mPath.lineTo(x3,y3);
        mPath.lineTo(x1,y1);

        //矩形区域的对象
        mRectF = new RectF(200, 200, 500, 300);

        mRectF1 = new RectF(100, 100, 700, 700);

        mArcPaint = new Paint();
        mArcPaint.setColor(Color.parseColor("#fa6a13"));
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStyle(Paint.Style.STROKE);//空心画笔
        mArcPaint.setStrokeWidth(25);

    }

    /**
     * @param canvas 画板/画布
     *               一般不要再绘制流程的方法中new对象
     */
    @Override
    protected void onDraw(Canvas canvas) {
        //6. 裁剪
        canvas.clipPath(mPath);
        //1.画线
        // canvas.drawLine(mStartX, mStartY, mStopX, mStopY, mPaint);
        //2. 画圆
        //canvas.drawCircle(mCx,mCy,mRadius,mPaint);
        //3.空心圆
        //canvas.drawCircle(mCx,mCy,mRadius,mCirclePaint);
        //4. 画图片
        canvas.drawBitmap(mBitmap,0,0,mPaint);
        //5. 画三角形(多边形)
        canvas.drawPath(mPath,mPaint);
        //7.椭圆
//        canvas.drawOval(mRectF,mPaint);
        //8.扇形
        //startAngle:起始角度,google规定,水平向右0度,垂直朝上-90/270,水平向下:90度
        //sweepAngle:扫过的角度
        //useCenter:true,扇形是完整的,false:扇形的三角区域就没有了,配合空心画笔就可以画出环
//        canvas.drawArc(mRectF1,0,60,false,mArcPaint);
    }
}
